package com.bank.user.service.impl;

import com.bank.common.exceptionUtil.CustomedException;
import com.bank.common.resultUtil.ResultCodeEnum;
import com.bank.model.entity.User;
import com.bank.model.vo.login.RegisterVo;
import com.bank.model.vo.user.UserVo;
import com.bank.user.service.FileService;
import com.bank.user.util.EntityFactory;
import com.bank.user.util.VoFactory;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * @author callumEm
 * @version 1.0
 * @create 2023/4/20 20:10
 */
@Service
public class FileServiceImpl implements FileService {
    @Autowired
    VoFactory voFactory;
    @Autowired
    EntityFactory entityFactory;


    @Override
    public List<UserVo> readUserFromExcel(MultipartFile multipartFile) throws IOException {
        String filePath = multipartFile.getOriginalFilename();
        String extString = filePath.substring(filePath.indexOf('.'));

        Workbook wb = null;

        InputStream input = multipartFile.getInputStream();
        if (".xls".equals(extString)) {
            wb = new HSSFWorkbook(input);
        } else if (".xlsx".equals(extString)) {
            wb = new XSSFWorkbook(input);
        } else {
            System.out.println("\n\n\n\n\n\n\n\n格式错误\n\n\n\n\n\n\n\n");
            throw new CustomedException(ResultCodeEnum.FILE_FORMAT_ERROR);
        }

        if (wb != null) {
            //用来存放表中数据
            List<UserVo> res = new ArrayList<>();
            //获取第一个sheet
            Sheet sheet = wb.getSheetAt(0);
            //获取最大行数
            int rowNum = sheet.getPhysicalNumberOfRows();
            //获取第一行
            Row row;
            for (int i = 0; i < rowNum; i++) {
                row = sheet.getRow(i);
                if (row != null) {
                    // 读取表格中数据，第一列
                    Cell cell = row.getCell(0);
                    //设置单元格类型
                    cell.setCellType(CellType.STRING);
                    String role = cell.getRichStringCellValue().toString();
                    if(role=="")break;
                    cell = row.getCell(1);
                    //设置单元格类型
                    cell.setCellType(CellType.STRING);
                    String name = cell.getRichStringCellValue().toString();
                    if(name=="")break;
                    cell = row.getCell(2);
                    cell.setCellType(CellType.STRING);
                    String sex = cell.getRichStringCellValue().toString();
                    if(sex=="")break;
                    cell = row.getCell(3);
                    cell.setCellType(CellType.STRING);
                    String phoneNumber = cell.getRichStringCellValue().toString();
                    if(phoneNumber=="")break;
                    cell = row.getCell(4);
                    cell.setCellType(CellType.STRING);
                    String birthdate = cell.getRichStringCellValue().toString();
                    if(birthdate=="")break;
                    cell = row.getCell(5);
                    cell.setCellType(CellType.STRING);
                    String identityNumber = cell.getRichStringCellValue().toString();
                    if(identityNumber=="")break;
                    cell = row.getCell(6);
                    cell.setCellType(CellType.STRING);
                    Integer age = Integer.parseInt(cell.getRichStringCellValue().toString());
                    if(age==null)break;

                    res.add(voFactory.getUserVo(role,name,sex,phoneNumber,birthdate,identityNumber,age));
                } else {
                    break;
                }
            }
            return res;
        }
        return null;
    }
}
